package com.leetcodehot.problems;

import java.util.*;

public class problems199 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private Map<Integer, Integer> map = new TreeMap<>();

    public List<Integer> rightSideView(TreeNode root) {
        dfs(root, 0);
        return new ArrayList<>(map.values());
    }

    private void dfs(TreeNode root, int d) {
        if (root == null) return;
        if (!map.containsKey(d)) {
            map.put(d, root.val);
        }
        dfs(root.right, d + 1);
        dfs(root.left, d + 1);
    }
}
